package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Response.Listener;
import com.bac.utils.Config;
import com.bac.utils.Util;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class NextPayPwdActivity extends BaseActivity implements OnClickListener{
	private EditText codeEditText;
	private EditText passwordEditText;
	private EditText confirmpwdEditText;
	private Button confirmButton;
	private String codeString;
	private String passwordString;
	private String confirmpwdString;
	private ImageButton clear_code;
	private ImageButton clear_pay_password;
	private ImageButton clear_pay_password2;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.paypwd_next);
	}

	@Override
	public void init() {
		confirmButton = (Button) findViewById(R.id.paypwd_ok_btn);
		confirmButton.setOnClickListener(this);
		
		codeEditText = (EditText) findViewById(R.id.paypwd_verify);
		codeEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (codeEditText.getText().toString() != null 
							&& !("").equals(codeEditText.getText().toString())) {
						clear_code.setVisibility(View.VISIBLE);
						clear_pay_password.setVisibility(View.INVISIBLE);
						clear_pay_password2.setVisibility(View.INVISIBLE);
					}
				}else {
					clear_code.setVisibility(View.INVISIBLE);
				}
			}
		});
		passwordEditText = (EditText) findViewById(R.id.pay_password);
		passwordEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (passwordEditText.getText().toString() != null 
							&& !("").equals(passwordEditText.getText().toString())) {
						clear_pay_password.setVisibility(View.VISIBLE);
						clear_code.setVisibility(View.INVISIBLE);
						clear_pay_password2.setVisibility(View.INVISIBLE);
					}
				}else {
					clear_pay_password.setVisibility(View.INVISIBLE);
				}
			}
		});
		confirmpwdEditText = (EditText) findViewById(R.id.pay_password2);
		confirmpwdEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (confirmpwdEditText.getText().toString() != null 
							&& !("").equals(confirmpwdEditText.getText().toString())) {
						clear_pay_password2.setVisibility(View.VISIBLE);
						clear_pay_password.setVisibility(View.INVISIBLE);
						clear_code.setVisibility(View.INVISIBLE);
					}
				}else {
					clear_pay_password2.setVisibility(View.INVISIBLE);
				}
			}
		});
		
		clear_code = (ImageButton) findViewById(R.id.btn_clear_code);
		clear_code.setOnClickListener(this);
		
		clear_pay_password = (ImageButton) findViewById(R.id.btn_clear_pay_password);
		clear_pay_password.setOnClickListener(this);
		
		clear_pay_password2 = (ImageButton) findViewById(R.id.btn_clear_pay_password2);
		clear_pay_password2.setOnClickListener(this);
	}
	
	TextWatcher watcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			
		}
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			
		}
		@Override
		public void afterTextChanged(Editable s) {
			if (codeEditText.hasFocus()
					&& codeEditText.getText().toString() != null
					&& !("").equals(codeEditText.getText().toString())) {
				clear_code.setVisibility(View.VISIBLE);
			}else {
				clear_code.setVisibility(View.INVISIBLE);
			}
			
			if (passwordEditText.hasFocus()
					&& passwordEditText.getText().toString() != null
					&& !("").equals(passwordEditText.getText().toString())) {
				clear_pay_password.setVisibility(View.VISIBLE);
			}else {
				clear_pay_password.setVisibility(View.INVISIBLE);
			}
			
			if (confirmpwdEditText.hasFocus()
					&& confirmpwdEditText.getText().toString() != null
					&& !("").equals(confirmpwdEditText.getText().toString())) {
				clear_pay_password2.setVisibility(View.VISIBLE);
			}else {
				clear_pay_password2.setVisibility(View.INVISIBLE);
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_clear_code:
			codeEditText.setText("");
			break;
		case R.id.btn_clear_pay_password:
			passwordEditText.setText("");
			break;
		case R.id.btn_clear_pay_password2:
			confirmpwdEditText.setText("");
			break;
		case R.id.paypwd_ok_btn:
			ResetPayPassword();
			break;

		default:
			break;
		}
		
	}

	private void ResetPayPassword() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sl = new Listener<String>() {

			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("TAG----success",response.toString());
				showToast("密码修改成功!");
				Intent intent = new Intent(NextPayPwdActivity.this,MainActivity.class);
				startActivity(intent);
			}
		};
		codeString = codeEditText.getText().toString().trim();
		passwordString = passwordEditText.getText().toString().trim();
		confirmpwdString = confirmpwdEditText.getText().toString().trim();
		if (codeString.length() <= 0) {
			showToast(R.string.long_hint_inputverify);
			return;
		}
		if (passwordString.length() <= 0 || confirmpwdString.length() <= 0) {
			showToast(R.string.password_none);
			return;
		}
		if (!(passwordString.equals(confirmpwdString))) {
			showToast(R.string.different_pwd);
			return;
		}
		
		showLoadingProgress();
		String path = Config.URL_RESET_PAY_PASSWORD;
		Map<String, String> map = new HashMap<String, String>();
		map.put("code", codeString);
		map.put("paymentPassword", passwordString);
		Util.httpStringPost(path, sl, this, map);
		
	}

}
